{% extends "common/flow.html" %}
{% block content %}
    <div class="layui-fluid">
        <div class="layui-card">
            <div class="layui-card-header">
                表单信息
            </div>
            <div class="layui-card-body">
                <form id="MAIN_DATA_FORM" class="layui-form">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label" style="width: 110px;">申请人</label>
                            <div class="layui-input-inline">
                                <input type="text" id="fcreatorname" name="fcreatorname" class="layui-input"
                                       readonly="readonly">
                            </div>
                        </div>
                        <div class="layui-inline" style="display: none;">
                            <label class="layui-form-label">申请人</label>
                            <div class="layui-input-inline">
                                <input type="text" id="fcreatorid" name="fcreatorid" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline" style="display: none;">
                            <label class="layui-form-label">申请人FID</label>
                            <div class="layui-input-inline">
                                <input type="text" id="fcreatorfid" name="fcreatorfid" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label" style="width: 110px;">请假类型</label>
                            <div class="layui-input-inline">
                                <select id="fleavetype" name="fleavetype" class="layui-input">
                                    <option>事假</option>
                                    <option>病假</option>
                                    <option>其他</option>
                                </select>
                            </div>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <div class="layui-inline" style="display: none;">
                            <label class="layui-form-label">申请人FNAME</label>
                            <div class="layui-input-inline">
                                <input type="text" id="fcreatorfname" name="fcreatorfname" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label" style="width: 110px;">申请时间</label>
                            <div class="layui-input-inline">
                                <input type="text" id="fcreatedate" name="fcreatedate" class="layui-input"
                                       readonly="readonly">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label" style="width: 110px;">请假天数</label>
                            <div class="layui-input-inline">
                                <input type="number" id="fday" name="fday" class="layui-input" lay-verify="required">
                            </div>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label" style="width: 110px;">开始时间</label>
                            <div class="layui-input-inline">
                                <input type="text" id="fstartdate" name="fstartdate" autocomplete="off"
                                       lay-verify="required"
                                       onClick="WdatePicker({dateFmt:'yyyy-MM-dd HH:mm:ss'})" class="layui-input Wdate">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label" style="width: 110px;">结束时间</label>
                            <div class="layui-input-inline">
                                <input type="text" id="fenddate" name="fenddate" autocomplete="off"
                                       lay-verify="required"
                                       onClick="WdatePicker({dateFmt:'yyyy-MM-dd HH:mm:ss'})" class="layui-input Wdate">
                            </div>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <div class="layui-inline" style="display: none;">
                            <label class="layui-form-label">申请状态</label>
                            <div class="layui-input-inline">
                                <input type="text" id="fstate" name="fstate" class="layui-input">
                            </div>
                        </div>
                    </div>
                    <div class="layui-form-item layui-form-text">
                        <label class="layui-form-label" style="width: 110px;">请假原因</label>
                        <div class="layui-input-block">
                            <textarea id="freason" name="freason" class="layui-textarea"></textarea>
                        </div>
                    </div>
                    <div class="layui-form-item layui-form-text">
                        <label class="layui-form-label" style="width: 110px;">附件</label>
                        <div class="layui-input-block" id="enclosure">
                        </div>
                    </div>
                    <div style="display: none">
                        <input type="text" id="fid" name="fid" class="layui-input" style="display: none;">
                        <button id="mainfsub" class="layui-btn" lay-submit lay-filter="mainform">立即提交</button>
                        <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                    </div>
                </form>
            </div>
        </div>
        <div class="layui-card">
            <div class="layui-card-header">
                审批信息&nbsp;<button class="layui-btn" onclick="tlv8.writeOpinion('flowOption')">填写审批意见</button>
            </div>
            <div class="layui-card-body" id="flowOption">
            </div>
        </div>
    </div>
    <script type="text/javascript">
        //工具条
        let toolbarItem, flwComponent;

        function init_toolbar() {
            let barDiv = J$("stander_bar");
            let toolbar = new tlv8.toolbar(barDiv, false, true, (activity === 'mainActivity'), true);
            toolbarItem = toolbar.items;
        }

        const MainData = new tlv8.Data();
        MainData.setTable("oa_leave");
        MainData.setFormId("MAIN_DATA_FORM");
        MainData.setQueryAction("/oa/leave/queryData");//定义数据查询动作
        MainData.setDeleteAction("/oa/leave/deleteData");//定义数据删除动作

        let activity = "{{ activity }}";

        if (activity !== 'mainActivity') {
            MainData.setReadonly(true); //如果不是第一个环节则设置表单只读
        }

        $(document).ready(function () {
            init_toolbar();
            flwComponent = new tlv8.flw("flowToolbar");

            if (flwComponent.taskID) {
                flwComponent.setItemStatus({
                    audit: false,
                    back: true,
                    out: true,
                    transmit: true,
                    pause: true,
                    stop: (activity === 'mainActivity')
                });
            }

            let rowid = tlv8.RequestURLParam.getParam("sData1");
            if (rowid && rowid !== "") {
                //这里做数据加载
                MainData.setRowId(rowid);
                MainData.refreshData(true, false);
                tlv8.loadOption("flowOption", rowid);//加载审批意见
            } else {
                initPsData();
            }

            new tlv8.fileComponent(J$("enclosure"), MainData, "fenclosure", "/root/请假申请/附件", true, true, true);
        });

        //保存表单数据
        function dataSave() {
            $("#mainfsub").click();
        }

        //删除数据动作
        function dataDeleted() {
            MainData.deleteData();
        }

        //初始化数据
        function initPsData() {
            $("#fcreatorname").val(tlv8.Context.getCurrentPersonName());
            $("#fcreatorid").val(tlv8.Context.getCurrentPersonID());
            $("#fcreatorfid").val(tlv8.Context.getCurrentPersonFID());
            $("#fcreatorfname").val(tlv8.Context.getCurrentPersonFName());
            $("#fcreatedate").val(tlv8.System.Date.sysDateTime());
        }

        //监听表单提交
        layui.form.on('submit(mainform)', function (data) {
            // layui.layer.msg(JSON.stringify(data.field));
            let param = new tlv8.RequestParam();
            param.set("field", JSON.stringify(data.field));
            let r = tlv8.XMLHttpRequest("/oa/leave/saveData", param, 'post', false);
            if (r.state === true) {
                layui.layer.msg("保存成功~");
                let rowid = r.rowid;
                if (!flwComponent.taskID || flwComponent.taskID === "") {
                    flwComponent.flowStart(rowid);
                }
            } else {
                layui.layer.alert(r.msg);
            }
            return false;
        });
    </script>
{% endblock %}